<?php

	/**
	 * Command Line Argument Parser
	 * 
	 * @author Michael T. Smith (michael@michaeltsmith.net)
	 * @copyright http://opensource.org/licenses/bsd-license.php New BSD License
	 * @version $Id: reader.php 9 2008-09-23 16:06:04Z michael@michaeltsmith.net $
	 */
	class reader {
		private $arguments;
		private $argumentArray;
		public $caller;
		
		function __construct($argv) {
			$this->arguments = $argv;
			$this->argumentArray = $this->chunkArguments($this->arguments);
		}
		
		/**
		 * Splits an argument string into an array
		 * 
		 * @param {String} argumentString
		 * @return {Array} argumentSet
		 */
		private function chunkArguments($argumentString) {
			$keyValuePairs = array();
			$keyValuePairs = split(' ', $this->arguments);
			$this->caller = $keyValuePairs[0];
			
			for ($i = 1; $i < count($keyValuePairs); $i++) {
				$keyValuePair = split('=', $keyValuePairs[$i]);
				$key = $this->cleanKey($keyValuePair[0]);
				
				if (preg_match('/;/', $keyValuePair[1])) {
					$argumentSet[$key] = split(';', $keyValuePair[1]);
				}
				else {
					$argumentSet[$key] = $keyValuePair[1];
				}
			}
			
			return $argumentSet;
		}
		
		/**
		 * Removes any dashes that prefix the key value of a parameter
		 * 
		 * @param {String} key
		 * @return {String}
		 */
		private function cleanKey($key) {
			return preg_replace('/^(--)/', '', $key);
		}
		
		/**
		 * Returns the arguments as the string they originally came from
		 *
		 * @return String
		 */
		public function __toString() {
			return $this->arguments;
		}
		
		/**
		 * Returns the arguments chunked as a string
		 * 
		 * @return {array}
		 */
		public function toArray() {
			return $this->argumentArray;
		}
	}

?>